<div class="cms_browse_heading">
	<div class="cms_browse_heading_label">
		<h1>View File</h1>
	</div>
</div>
<div id="cms_browse_topdivider" class="cms_browse_divider"></div>
<div id="cms_browse">	
<?

	include_once("cmsFiles.php");

	$documentCode = extractNumberWithKeyFromArray('documentCode', $_REQUEST);
	$folder = extractNumberWithKeyFromArray('folder', $_REQUEST);
	
	$document = $cmsDocument->openDocument($documentCode);
	$formDescriptionTableWriter = new CmsTableWriter("formdescriptor", $db);
	$formDescription = $formDescriptionTableWriter->readItem( 147 );
	//if (!$formDescription) die("huh.. excellent error about this should not happen here");


	
	if (!is_null($document) && $document->formid == 147){
		
		
		$documentDetails = getFileDetails($document, $db );
		if (is_null($documentDetails)) {
			$documentDetails = new stdClass();//create this to pad out the bollocks
			$documentDetails->filename = "NO FILE DATA FOUND";
			$documentDetails->filesize = 0;
			$documentDetails->id  = 0;
			$documentDetails->mimetype= 0;
		}
		
		$documentTitle = magicClean($document->title);
		$documentDescription = magicClean($document->shortdescription);			
		$documentDate = magicClean($document->documentdate);
		
		$documentFilename = magicClean($documentDetails->filename);

		$returnURL = $_SERVER['PHP_SELF'] . "?fileNo=1&formid=147&sorts=clear&direction=clear&folder=$folder";		
		$cancelURL = $_SERVER['PHP_SELF'] . "?fileNo=102&documentCode=". $documentCode . "&folder=" . $folder;		
		$editUrl = $_SERVER['PHP_SELF'] . '?fileNo=2&documentCode=' . $document->documentcode . "&returnURL=" . urlencode($cancelURL);
		$deleteUrl = $_SERVER['PHP_SELF'] . '?fileNo=4&documentCode=' . $document->documentcode . "&returnURLEncoded=" . urlencode( $returnURL ) . "&cancelURLEncoded=" . urlencode( $cancelURL );
	
		if ($documentDetails->id === 0 ) {
			$downloadUrl = null;
		} else  {
			$imageUrl = '/html/blob.php?attach=false&documentCode=' . $document->documentcode .  '&scale=1&width=300';			
			$downloadUrl = '/html/blob.php?attach=true&documentCode=' . $document->documentcode ;
		}

		//get the details of the category??
	
		//try and determine mime types for icon.. move this out later

		$iconUrl = iconForMimeType($documentDetails->mimetype);
		
		print <<< EOD
        <table cellspacing="0" cellpadding="0" width="560" border="0">
          <tr> 
            <td align="left" valign="top" class="cms_browsetext"><b>$documentTitle</b><br />
              $documentFilename <br /><br />
              $documentDescription<br /> <br /> <br />
EOD;
		if ($documentDetails->id !== 0) {
				print <<< EOD
              <img src="$iconUrl" width="35" height="20" align="left" />
             	<a href="$downloadUrl">Download/Save file</a> <br /> <i>($documentDetails->filesize bytes)</i><br />
EOD;
		}
		print <<< EOD
             	<br />
              <br />
			</td>
            <td valign="top" width="120">
EOD;
				switch ($documentDetails->mimetype){
					
					case 'image/jpeg':
					case 'image/pjpeg':
					case 'image/gif':
					case 'image/png':
						print <<< EOD
            	<div class="wrap1"> 
                <div class="wrap2"> 
                  <div class="wrap3"><img src="$imageUrl" alt="description" border="0" /></div>
                </div>
              </div>
EOD;
					break;
				
				}
				print <<< EOD
			</td>
          </tr>
		  <tr>
		  	<td colspan="2">
              <img height="7" src="/cms/images/greyline.gif" width="100%" /><br />
              <i>
			  Date added: $documentDate<br />
              Document Code: $document->documentcode<br />
              File Id: $documentDetails->id <br />
              File URL: /html/blob.php?attach=true&documentCode=$document->documentcode<br />
              <br />
              </i><img height="7" src="/cms/images/greyline.gif" width="100%" /><br />
EOD;
				if ( editFormAuth($authInfo, $formDescription, $row->documentcode, $db)) {
					print <<< EOD
              <a href="$editUrl">Edit</a> 
EOD;
				}
				if ( deleteFormAuth($authInfo, $formDescription, $row->documentcode, $db)) {
					print <<< EOD
             / 
             <a href="$deleteUrl">Delete</a>
EOD;
				}
				print <<< EOD
			</td>
		  </tr>	  
        </table>
EOD;

	} else {
		echo "FILE NOT FOUND";
	}

	$upUrl = $_SERVER['PHP_SELF'] . '?fileNo=1&formid=147&folder=' . $folder ;
?>
</div>
<table cellpadding="0" cellspacing="0" width="100%" border="0">
  <tr> 
    <td align="left" colspan="2"><img height="7" src="/cms/images/greyline.gif" width="100%" /></td>
  </tr>
  <tr> 
    <td class="cms_browsetext" width="35"><a href="<?= $upUrl ?>"><img src="/cms/html/files/images/icon_folderup.gif" width="35" height="20" /></a></td>
    <td class="cms_browsetext" ><a href="<?= $upUrl ?>"><i>.. Back to file folder.</i></a></td>
  </tr>
  <tr> 
    <td align="left" colspan="2"><img height="7" src="/cms/images/greyline.gif" width="100%" /></td>
  </tr>
</table>
